package com.lanyou.esb.cook.proxy.service.impl;

import java.util.List;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.lanyou.esb.cook.proxy.dao.impl.BaseDao;
import com.lanyou.esb.cook.proxy.entity.StoreForward;
import com.lanyou.esb.cook.proxy.redis.proxy.impl.StoreForwardJedisProxy;
import com.lanyou.esb.cook.proxy.redis.proxy.impl.StoreForwardKeysJedisProxy;
import com.lanyou.esb.cook.proxy.service.Service;
import com.lanyou.esb.cook.proxy.service.StoreForwardService;
import com.lanyou.esb.cook.proxy.service.push.Push;

/**
 * 存储转发统计服务
 * 
 * @author Davey.wu
 */
public class StoreForwardServiceImpl implements StoreForwardService,
		Service<StoreForward> {
	/** 数据库操作接口 **/
	private BaseDao<StoreForward> dao;
	/** 存储转发统计表Redis操作代理 **/
	private StoreForwardJedisProxy jedisProxy;
	/** 存储转发统计表键值Redis操作代理 **/
	private StoreForwardKeysJedisProxy storeForwardKeysJedisProxy;
	/** 消息推送接口 **/
	private Push<StoreForward> push;

	/** 本地线程变量，用于保存当前线程的请求转发统计信息 **/
	private static ThreadLocal<StoreForward> THREADLOCAL_STOREFORWARD = new ThreadLocal<>();

	@Override
	public void pushToRedis() {
		// 获取当前线程的跟踪日志
		StoreForward reqForward = THREADLOCAL_STOREFORWARD.get();
		if (reqForward == null) {
			return;
		}
		try {
			String key = jedisProxy.getKeyFromObject(reqForward);
			// 保存键值
			storeForwardKeysJedisProxy.saddAsStr(
					storeForwardKeysJedisProxy.getKey(), key);
			// 将跟踪信息存入REDIS
			jedisProxy.setex(key, reqForward);
		} catch (Exception e) {
			push.putToCollection4Redis(reqForward);
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void save(final List<StoreForward> list) throws Exception {
		if (list == null || list.isEmpty()) {
			return;
		}
		try {
			TransactionTemplate tt = new TransactionTemplate(
					this.dao.getTransactionManager());
			tt.execute(new TransactionCallback() {
				public Object doInTransaction(TransactionStatus status) {
					return StoreForwardServiceImpl.this.dao.saveAll(list);
				}
			});
		} catch (Exception e) {
			// 保留失败，将记录添加到推送队列中
			push.putToCollection4DB(list);
		}
	}

	@Override
	public void update(List<StoreForward> list) throws Exception {
		// 暂时没有更新业务
	}

	/**
	 * 清理数据
	 */
	public static void clear() {
		THREADLOCAL_STOREFORWARD.remove();
	}

	/**
	 * 获取请求转发信息
	 * 
	 * @return
	 */
	public static StoreForward get() {
		StoreForward reqForward = THREADLOCAL_STOREFORWARD.get();
		if (reqForward == null) {
			reqForward = new StoreForward();
			THREADLOCAL_STOREFORWARD.set(reqForward);
		}
		return reqForward;
	}

	/**
	 * 设置请求转发信息
	 * 
	 * @param reqForward
	 */
	public static void set(StoreForward reqForward) {
		THREADLOCAL_STOREFORWARD.set(reqForward);
	}

	public StoreForwardJedisProxy getJedisProxy() {
		return jedisProxy;
	}

	public void setJedisProxy(StoreForwardJedisProxy jedisProxy) {
		this.jedisProxy = jedisProxy;
	}

	public StoreForwardKeysJedisProxy getStoreForwardKeysJedisProxy() {
		return storeForwardKeysJedisProxy;
	}

	public void setStoreForwardKeysJedisProxy(
			StoreForwardKeysJedisProxy storeForwardKeysJedisProxy) {
		this.storeForwardKeysJedisProxy = storeForwardKeysJedisProxy;
	}

	public BaseDao<StoreForward> getDao() {
		return dao;
	}

	public void setDao(BaseDao<StoreForward> dao) {
		this.dao = dao;
	}

	public Push<StoreForward> getPush() {
		return push;
	}

	public void setPush(Push<StoreForward> push) {
		this.push = push;
	}
}
